#include "CorePch.h"
#include "JObject.h"
//

const  JRtti JObject::m_type = JRtti(L"JJc.Object",NULL);
uint64 JObject::m_instansIdCount = 0;
JObject::InstansMap* JObject::m_pInstansMap = 0;
JObject::InstansMap g_instansMap;

JJc::JObject::JObject()
{
	m_pInstansMap = &g_instansMap; 
	m_instansId = m_instansIdCount++;
	(*m_pInstansMap)[m_instansId] = this;
	m_referenceCount = 0;
}

bool JJc::JObject::IsTypeOf( const JObject* pObject) const
{
	return GetType().IsTypeOf( pObject->GetType());
}

bool JJc::JObject::IsTypeOf( const JRtti& type) const  
{
	return GetType().IsTypeOf(type);  
}

bool JJc::JObject::IsDerivedOf( const JObject* pObject) const
{
	return GetType().IsDerived(pObject->GetType());
}

bool JJc::JObject::IsDerivedOf( const JRtti& type) const
{
	return GetType().IsDerived(type);
}
const JRtti& JJc::JObject::GetType()const
{
	return m_type;
}

uint64 JJc::JObject::GetID() const
{
	return m_instansId;
}

uint64 JJc::JObject::GetNextID()
{
	return m_instansIdCount+1;
}


JJc::JObject::~JObject()
{

}

JObject* JJc::JObject::GetObjectByID(uint64 instanceId)
{
	return (*m_pInstansMap)[instanceId];
}

uint64 JJc::JObject::GetReference() const
{
	return m_referenceCount;
}

void JJc::JObject::IncreaseReference()
{
	m_referenceCount++;
}

void JJc::JObject::DecreaseReference()
{
	if(m_referenceCount--==0)
	{
		delete this;
	}
}